using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class TheoDoiSXControl : BUSControl
    {
		#region Local Variable
        private TheoDoiSXDataAccess _objDAO;
		#endregion Local Variable

	    public TheoDoiSXControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new TheoDoiSXDataAccess(Connnection); }
			
        public TheoDoiSXControl(BUSControl common)
            : base(common) { _objDAO = new TheoDoiSXDataAccess(common.Connnection); }
	
		#region Method
		public DataTable GetShortData(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetShortData(IdSX, XuongId, filters, ref sErr);
        }
       		
        public DataTable GetByFilterToDataTable(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            return _objDAO.GetByFilterToDataTable(IdSX, XuongId, filters, ref logMsg, indexPage, itemPerPage);
        }
		
		public int GetCountRecord(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetCountRecord(IdSX, XuongId, filters, ref sErr);
        }
		
		public TheoDoiSXInfo[] GetByFilter(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            TheoDoiSXInfo[] res = _objDAO.GetByFilter(IdSX, XuongId, filters, ref logMsg, indexPage, itemPerPage);
			//get details
			
			return res;
        }
		
        public int Add(TheoDoiSXInfo obj, ref string sErr) {
            _objDAO.Add(obj, ref sErr);
			if(sErr == "") {
				//add details
			}
			return 1;
        }
		
        public string Update(TheoDoiSXInfo obj) {
            string sErr = _objDAO.Update(obj);
			// delete details
			// add details
			return sErr;
        }
		
        public string Delete(Int64 IdSX) {
            string sErr = "";
			// delete details
			
			if(sErr == "") sErr = _objDAO.Delete(IdSX);
			return sErr;
        }  
        public bool IsExist(Int64? IdSX, ref string sErr) {
            return _objDAO.IsExist(IdSX, ref sErr);
        } 

        public string InsertUpdate(TheoDoiSXInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.IdSX, ref sErr)) { sErr = Update(obj); }
            else { 
				if(sErr =="") Add(obj, ref sErr); 
			}
            return sErr;
        }		
      
		public string TransferIn(DataRow row) {
            TheoDoiSXInfo inf = new TheoDoiSXInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr) {
            string result = "";
            try {
                if (lInf != null && lInf.Length > 0) {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("TheoDoiSX");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf) {
                        DTO.TheoDoiSXInfo it = x as DTO.TheoDoiSXInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

/* Can not combine this                         if (it.<%PrimaryNotAccColumn(Name)%> != null) row.SetAttribute("<%PrimaryNotAccColumn(Name)%>", string.Format("{0}", it.<%PrimaryNotAccColumn(Name)%>));*/
						if (it.XuongId != null) row.SetAttribute("XuongId", string.Format("{0}", it.XuongId));
						if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
						if (it.ThoiGian != null) row.SetAttribute("ThoiGian", string.Format("{0}", it.ThoiGian));
						if (it.LoaiBang != null) row.SetAttribute("LoaiBang", string.Format("{0}", it.LoaiBang));
						if (it.TenChuyen != null) row.SetAttribute("TenChuyen", string.Format("{0}", it.TenChuyen));
						if (it.KhachHang != null) row.SetAttribute("KhachHang", string.Format("{0}", it.KhachHang));
						if (it.MaHang != null) row.SetAttribute("MaHang", string.Format("{0}", it.MaHang));
						if (it.SoLuongMaHang != null) row.SetAttribute("SoLuongMaHang", string.Format("{0}", it.SoLuongMaHang));
						if (it.DonGia != null) row.SetAttribute("DonGia", string.Format("{0}", it.DonGia));
						if (it.SoLaoDong != null) row.SetAttribute("SoLaoDong", string.Format("{0}", it.SoLaoDong));
						if (it.SoVang != null) row.SetAttribute("SoVang", string.Format("{0}", it.SoVang));
						if (it.DinhMuc1 != null) row.SetAttribute("DinhMuc1", string.Format("{0}", it.DinhMuc1));
						if (it.DinhMuc2 != null) row.SetAttribute("DinhMuc2", string.Format("{0}", it.DinhMuc2));
						if (it.DinhMuc3 != null) row.SetAttribute("DinhMuc3", string.Format("{0}", it.DinhMuc3));
						if (it.DinhMuc4 != null) row.SetAttribute("DinhMuc4", string.Format("{0}", it.DinhMuc4));
						if (it.DinhMuc5 != null) row.SetAttribute("DinhMuc5", string.Format("{0}", it.DinhMuc5));
						if (it.HoanTat1 != null) row.SetAttribute("HoanTat1", string.Format("{0}", it.HoanTat1));
						if (it.HoanTat2 != null) row.SetAttribute("HoanTat2", string.Format("{0}", it.HoanTat2));
						if (it.HoanTat3 != null) row.SetAttribute("HoanTat3", string.Format("{0}", it.HoanTat3));
						if (it.HoanTat4 != null) row.SetAttribute("HoanTat4", string.Format("{0}", it.HoanTat4));
						if (it.HoanTat5 != null) row.SetAttribute("HoanTat5", string.Format("{0}", it.HoanTat5));
						if (it.LuyKeSL1 != null) row.SetAttribute("LuyKeSL1", string.Format("{0}", it.LuyKeSL1));
						if (it.LuyKeSL2 != null) row.SetAttribute("LuyKeSL2", string.Format("{0}", it.LuyKeSL2));
						if (it.LuyKeSL3 != null) row.SetAttribute("LuyKeSL3", string.Format("{0}", it.LuyKeSL3));
						if (it.LuyKeSL4 != null) row.SetAttribute("LuyKeSL4", string.Format("{0}", it.LuyKeSL4));
						if (it.LuyKeSL5 != null) row.SetAttribute("LuyKeSL5", string.Format("{0}", it.LuyKeSL5));
						if (it.KeHoachSL1 != null) row.SetAttribute("KeHoachSL1", string.Format("{0}", it.KeHoachSL1));
						if (it.KeHoachSL2 != null) row.SetAttribute("KeHoachSL2", string.Format("{0}", it.KeHoachSL2));
						if (it.KeHoachSL3 != null) row.SetAttribute("KeHoachSL3", string.Format("{0}", it.KeHoachSL3));
						if (it.KeHoachSL4 != null) row.SetAttribute("KeHoachSL4", string.Format("{0}", it.KeHoachSL4));
						if (it.WorkTimeOfDay != null) row.SetAttribute("WorkTimeOfDay", string.Format("{0}", it.WorkTimeOfDay));
						if (it.WorkTimeOfNow != null) row.SetAttribute("WorkTimeOfNow", string.Format("{0}", it.WorkTimeOfNow));
						// details
						//if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode) {
            string sErr = "";
            if (inf is TheoDoiSXInfo) {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.TheoDoiSXInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((TheoDoiSXInfo)inf);
                else sErr = InsertUpdate((TheoDoiSXInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml,ref string sErr, string db = null) {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++) {
                if (sErr != "") return lRes;
                try {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;
               
                    DTO.TheoDoiSXInfo inf = new DTO.TheoDoiSXInfo();
					
					inf.XuongId = db;
					if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
					if (GetStringAttribute(ele, "ThoiGian") != "") inf.ThoiGian = GetDateTimeAttribute(ele, "ThoiGian");
					if (GetStringAttribute(ele, "LoaiBang") != "") inf.LoaiBang = GetStringAttribute(ele, "LoaiBang");
					if (GetStringAttribute(ele, "TenChuyen") != "") inf.TenChuyen = GetStringAttribute(ele, "TenChuyen");
					if (GetStringAttribute(ele, "KhachHang") != "") inf.KhachHang = GetStringAttribute(ele, "KhachHang");
					if (GetStringAttribute(ele, "MaHang") != "") inf.MaHang = GetStringAttribute(ele, "MaHang");
					if (GetStringAttribute(ele, "SoLuongMaHang") != "") inf.SoLuongMaHang = GetInt64Attribute(ele, "SoLuongMaHang");
					if (GetStringAttribute(ele, "DonGia") != "") inf.DonGia = GetDoubleAttribute(ele, "DonGia");
					if (GetStringAttribute(ele, "SoLaoDong") != "") inf.SoLaoDong = GetInt32Attribute(ele, "SoLaoDong");
					if (GetStringAttribute(ele, "SoVang") != "") inf.SoVang = GetInt32Attribute(ele, "SoVang");
					if (GetStringAttribute(ele, "DinhMuc1") != "") inf.DinhMuc1 = GetInt32Attribute(ele, "DinhMuc1");
					if (GetStringAttribute(ele, "DinhMuc2") != "") inf.DinhMuc2 = GetInt32Attribute(ele, "DinhMuc2");
					if (GetStringAttribute(ele, "DinhMuc3") != "") inf.DinhMuc3 = GetInt32Attribute(ele, "DinhMuc3");
					if (GetStringAttribute(ele, "DinhMuc4") != "") inf.DinhMuc4 = GetInt32Attribute(ele, "DinhMuc4");
					if (GetStringAttribute(ele, "DinhMuc5") != "") inf.DinhMuc5 = GetInt32Attribute(ele, "DinhMuc5");
					if (GetStringAttribute(ele, "HoanTat1") != "") inf.HoanTat1 = GetInt32Attribute(ele, "HoanTat1");
					if (GetStringAttribute(ele, "HoanTat2") != "") inf.HoanTat2 = GetInt32Attribute(ele, "HoanTat2");
					if (GetStringAttribute(ele, "HoanTat3") != "") inf.HoanTat3 = GetInt32Attribute(ele, "HoanTat3");
					if (GetStringAttribute(ele, "HoanTat4") != "") inf.HoanTat4 = GetInt32Attribute(ele, "HoanTat4");
					if (GetStringAttribute(ele, "HoanTat5") != "") inf.HoanTat5 = GetInt32Attribute(ele, "HoanTat5");
					if (GetStringAttribute(ele, "LuyKeSL1") != "") inf.LuyKeSL1 = GetInt32Attribute(ele, "LuyKeSL1");
					if (GetStringAttribute(ele, "LuyKeSL2") != "") inf.LuyKeSL2 = GetInt32Attribute(ele, "LuyKeSL2");
					if (GetStringAttribute(ele, "LuyKeSL3") != "") inf.LuyKeSL3 = GetInt32Attribute(ele, "LuyKeSL3");
					if (GetStringAttribute(ele, "LuyKeSL4") != "") inf.LuyKeSL4 = GetInt32Attribute(ele, "LuyKeSL4");
					if (GetStringAttribute(ele, "LuyKeSL5") != "") inf.LuyKeSL5 = GetInt32Attribute(ele, "LuyKeSL5");
					if (GetStringAttribute(ele, "KeHoachSL1") != "") inf.KeHoachSL1 = GetInt32Attribute(ele, "KeHoachSL1");
					if (GetStringAttribute(ele, "KeHoachSL2") != "") inf.KeHoachSL2 = GetInt32Attribute(ele, "KeHoachSL2");
					if (GetStringAttribute(ele, "KeHoachSL3") != "") inf.KeHoachSL3 = GetInt32Attribute(ele, "KeHoachSL3");
					if (GetStringAttribute(ele, "KeHoachSL4") != "") inf.KeHoachSL4 = GetInt32Attribute(ele, "KeHoachSL4");
					if (GetStringAttribute(ele, "WorkTimeOfDay") != "") inf.WorkTimeOfDay = GetInt32Attribute(ele, "WorkTimeOfDay");
					if (GetStringAttribute(ele, "WorkTimeOfNow") != "") inf.WorkTimeOfNow = GetInt32Attribute(ele, "WorkTimeOfNow");
					// details
					//string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch(Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
		public bool validate(JObject o, bool pkOnly, ref string sErr) {
            bool inputErr = false;
			if (pkOnly) {
/* Can not combine this                 if (o["<%PrimaryNotAccColumn(Name)%>"] == null) { sErr += "Invalid <%PrimaryNotAccColumn(Name)%>. Please check again"; inputErr = true; }*/
            } else {
/* Can not combine this 				if (o["<%PrimaryNotAccColumn(Name)%>"] == null) { sErr += "Invalid <%PrimaryNotAccColumn(Name)%>. Please check again"; inputErr = true; }*/
				if (o["XuongId"] != null) { }//Check validate 
				if (o["LineId"] != null) { }//Check validate 
				if (o["ThoiGian"] != null) { }//Check validate 
				if (o["LoaiBang"] != null) { }//Check validate 
				if (o["TenChuyen"] != null) { }//Check validate 
				if (o["KhachHang"] != null) { }//Check validate 
				if (o["MaHang"] != null) { }//Check validate 
				if (o["SoLuongMaHang"] != null) { }//Check validate 
				if (o["DonGia"] != null) { }//Check validate 
				if (o["SoLaoDong"] != null) { }//Check validate 
				if (o["SoVang"] != null) { }//Check validate 
				if (o["DinhMuc1"] != null) { }//Check validate 
				if (o["DinhMuc2"] != null) { }//Check validate 
				if (o["DinhMuc3"] != null) { }//Check validate 
				if (o["DinhMuc4"] != null) { }//Check validate 
				if (o["DinhMuc5"] != null) { }//Check validate 
				if (o["HoanTat1"] != null) { }//Check validate 
				if (o["HoanTat2"] != null) { }//Check validate 
				if (o["HoanTat3"] != null) { }//Check validate 
				if (o["HoanTat4"] != null) { }//Check validate 
				if (o["HoanTat5"] != null) { }//Check validate 
				if (o["LuyKeSL1"] != null) { }//Check validate 
				if (o["LuyKeSL2"] != null) { }//Check validate 
				if (o["LuyKeSL3"] != null) { }//Check validate 
				if (o["LuyKeSL4"] != null) { }//Check validate 
				if (o["LuyKeSL5"] != null) { }//Check validate 
				if (o["KeHoachSL1"] != null) { }//Check validate 
				if (o["KeHoachSL2"] != null) { }//Check validate 
				if (o["KeHoachSL3"] != null) { }//Check validate 
				if (o["KeHoachSL4"] != null) { }//Check validate 
				if (o["WorkTimeOfDay"] != null) { }//Check validate 
				if (o["WorkTimeOfNow"] != null) { }//Check validate 
			}
			return inputErr;
		}
		public override bool isField(string field) {
            return new DTO.TheoDoiSXInfo().isField(field);
        }
		#endregion Method

    }
}
